package com.jingzhi.huimiao;

import java.sql.Date;
import java.util.ArrayList;
import java.util.logging.LogRecord;

import com.jingzhi.huimiao.bean.WordInfo;
import com.jingzhi.huimiao.utils.DataStoreUtil;
import com.jingzhi.huimiao.utils.SQLdm;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 学习页面
 * 
 * @author cxk
 * 
 * 
 */
public class StudyActivity extends Activity {

	private ViewPager vp_study;
	private int ids[];
	private TextView tv_title;
	private LinearLayout iv_back_tomain;
	private ProgressBar pb_study;
	private SQLiteDatabase db;
	private ArrayList<WordInfo> wordlist;
	private Cursor cursor;
	private StudyAdapter adapter;
	private int WHAT = 0;
	
	private Handler hander = new Handler() {
		
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				
				break;

			default:
				break;
			}
		};
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_study);

		adapter = new StudyAdapter();
		pb_study = (ProgressBar) findViewById(R.id.pb_study);
		tv_title = (TextView) findViewById(R.id.tv_title);
		iv_back_tomain = (LinearLayout) findViewById(R.id.iv_back_tomain);
		
		iv_back_tomain.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(StudyActivity.this, TaskActivity.class));
				finish();
			}
		});
		
		long taskid = DataStoreUtil.getLong(StudyActivity.this, "CHOOSETASK");
		// 设置TestActivity的标题
		tv_title.setText("Task" + "" + ((taskid - 57) % 3 + 1));

		pb_study.setProgress(0);

		// 打开数据库输出流
		SQLdm s = new SQLdm();
		db = s.openDatabase(getApplicationContext());

		// 查询数据库中taskid的数据
		wordlist = new ArrayList<WordInfo>();

		cursor = db.rawQuery("select * from word where pid=?",
				new String[] { taskid + "" });
		Log.e("Taskid--------test", taskid + "");
		String name = null;
		String translation = null;
		String example = null;

		while (cursor.moveToNext()) {
			WordInfo wordinfo = new WordInfo();
			wordinfo.id = cursor.getLong(cursor.getColumnIndex("id"));
			wordinfo.wname = cursor.getString(cursor
					.getColumnIndex("word_name"));
			wordinfo.wexamp = cursor.getString(cursor
					.getColumnIndex("word_example"));
			wordinfo.wtrans = cursor.getString(cursor
					.getColumnIndex("word_translation"));
			wordinfo.wex_tr = cursor.getString(cursor
					.getColumnIndex("word_example_translation"));
			wordinfo.wsoundm = cursor.getString(cursor
					.getColumnIndex("word_soundmark"));
			wordinfo.pid = cursor.getLong(cursor.getColumnIndex("pid"));
			wordlist.add(wordinfo);

			// Toast.makeText(TestActivity.this,wordinfo.wname, 0).show();
		}
		if(cursor!=null){
			cursor.close();
		}if(db!=null){
			db.close();
		}

		pb_study.setMax(wordlist.size());
		vp_study = (ViewPager) findViewById(R.id.vp_study);

		ids = new int[] { R.drawable.bg__01, R.drawable.bg__02,
				R.drawable.bg__03, R.drawable.bg__04, R.drawable.bg__05,
				R.drawable.bg__06 };

		vp_study.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
				// TODO Auto-generated method stub
			
				pb_study.setProgress(position+1);
			}
			
			@Override
			public void onPageScrollStateChanged(int state) {
				// TODO Auto-generated method stub
				
			}
		});
		
	
		vp_study.setAdapter(adapter);
		
		

	}

	
	
	class StudyAdapter extends PagerAdapter implements OnClickListener {

		private LinearLayout ll_study;
		private TextView word_name;
		

		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return wordlist.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			// TODO Auto-generated method stub
			return object == view;
		}

		@Override
		public Object instantiateItem(ViewGroup container, final int position) {
			// TODO Auto-generated method stub
			final View view = View.inflate(StudyActivity.this, R.layout.study_item,
					null);
			final ImageView word_save = (ImageView) view
					.findViewById(R.id.iv_studysave);
			ImageView word_sound = (ImageView) view
					.findViewById(R.id.iv_study_sound);
			word_name = (TextView) view
					.findViewById(R.id.tv_study_word);
			TextView word_soundmark = (TextView) view
					.findViewById(R.id.tv_study_soundmark);
			TextView word_translation = (TextView) view
					.findViewById(R.id.tv_study_translation);
			TextView word_exaptrans = (TextView) view
					.findViewById(R.id.tv_study_exaptrans);
			ll_study = (LinearLayout) view.findViewById(R.id.ll_study);

//			 ll_study.setVisibility(View.VISIBLE);
			word_name.setText(wordlist.get(position).wname);
			word_soundmark.setText(wordlist.get(position).wsoundm);
			word_translation.setText(wordlist.get(position).wtrans);
			word_exaptrans.setText(wordlist.get(position).wexamp
					+ wordlist.get(position).wex_tr);
			
			// 给ll_study设置点击监听
			ll_study.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Toast.makeText(StudyActivity.this, "点击了。。。。。", 0).show();
					ll_study.setBackgroundColor(Color.BLACK);
//					word_name.setText(wordlist.get(0).wtrans);
//					view.setVisibility(View.INVISIBLE);
					Toast.makeText(StudyActivity.this, "又点击了。。。。。", 1).show();
				}
			});
			word_save.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
//					Toast.makeText(StudyActivity.this, "点击了。。。。。", 0).show();
//					word_name.setText(wordlist.get(0).wtrans);
//					view.setVisibility(View.INVISIBLE);
					word_save.setBackgroundResource(R.drawable.icon_save1);
					
					// 打开数据库输出流
					SQLdm s = new SQLdm();
					db = s.openDatabase(getApplicationContext());
					
					ContentValues values = new ContentValues();
					values.put("word_right", 1);// key为字段名，value为值
					db.update("word", values, "id=?",
							new String[] { wordlist.get(position).id + "" });

					long wordid = wordlist.get(position).id;
					long user_id = DataStoreUtil.getLong(StudyActivity.this, "userid");
					Date date = new Date(System.currentTimeMillis());
//					String str = formatter.format(date); 
					Log.e("time", date+"-----");
					// String date=formatter.format(new java.util.Date());
					int wrongtype=0;
					//判断生词本中是否有该单词
					Cursor isinwrong = db.rawQuery("select * from word where id='"+wordid+"';", null);
					if(isinwrong.moveToFirst()) {
						wrongtype = isinwrong.getInt(isinwrong.getColumnIndex("wrong_type"));
						Log.e("TAG---wrongtype", wrongtype+"");
					}
					if(wrongtype == 0){
						db.execSQL("insert into wrong_word (user_id,word_id,insert_time) values ("
								+ user_id + "," + wordid + ",'" + date + "');");
						//更新wrongtype为1
						db.execSQL("update word set wrong_type=1 where id='"+wordid+"';");
					}else{
						//根据单词ID更新wrong_word的插入时间
						db.execSQL("update wrong_word set insert_time='"+date+"' where word_id="+wordid+";");
					}
					if(isinwrong!=null){
						isinwrong.close();
					}if(db!=null){
						db.close();
					}
					
					Toast.makeText(StudyActivity.this, "已收藏", 0).show();
				}

				
			});
			container.addView(view);
			return view;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			// super.destroyItem(container, position, object);
			container.removeView((View) object);
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.ll_study:
				
				break;

			default:
				break;
			}
		}

	}
}
